package com.zkx.fssb.controller.server;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkx.fssb.domain.dto.CakeDto;
import com.zkx.fssb.domain.po.Cake;
import com.zkx.fssb.result.PageQuery;
import com.zkx.fssb.result.Result;
import com.zkx.fssb.service.CakeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@Slf4j
@RestController("serverCakeController")
@RequestMapping("/server/cake")
public class CakeController {

    @Autowired
    private CakeService cakeService;


    @GetMapping
    public Result<List<CakeDto>> getList(){

        List<Cake> cakeList = cakeService.getBaseMapper().selectList(null);
        List<CakeDto> cakeDtoList = cakeList.stream().map((item) -> {
            CakeDto cakeDto = new CakeDto();
            BeanUtils.copyProperties(item, cakeDto);
            return cakeDto;
        }).collect(Collectors.toList());
        return Result.success(cakeDtoList);


    }


    @GetMapping("getById")
    public Result<CakeDto> getById(Integer cakeId){
        Cake cake = cakeService.getById(cakeId);
        CakeDto cakeDto = new CakeDto();
        BeanUtils.copyProperties(cake,cakeDto);
        return Result.success(cakeDto);


    }

    @GetMapping("/page")
    public Result<Page> getPage(PageQuery pageQuery){
        log.info(pageQuery.toString());
        Page page =cakeService.customPage(pageQuery);



        return Result.success(page);


    }



}
